#!/bin/bash

Install_Kibana()
{
    Echo_Info "====== Installing Kibana ======"
    Echo_Info "Install ${Kibana_Ver} Version..."
	source /etc/profile
	Echo_Info ${PATH}
    Press_Start

    cd ${cur_dir}/src
	
	#下载安装Kibana
	Kibana_Install

	#把Kibana安装为linux服务
	Kibana_Add_AutoStartup
}

Kibana_Install(){
    Echo_Info "====== Kibana_Install ======"
	
	Download_Files ${YUSP_Download_Mirror}/kibana-${Kibana_Ver}-linux-x86_64.tar.gz kibana-${Kibana_Ver}-linux-x86_64.tar.gz
	rm -rf ${ELK_Install_Dir}/kibana*
	Echo_Info "正在解压Kibana......"
	tar -xf kibana-${Kibana_Ver}-linux-x86_64.tar.gz -C ${ELK_Install_Dir}

	#启动Kibana
	# CURRNET_IP=`hostname -I | sed  's/[ \t]*$//g'`
	# ELASTICSEARCHURL="http://${CURRNET_IP}:9200"
	# Echo_Info "当前主机IP======>${CURRNET_IP}"
	# Echo_Info "ELASTICSEARCHURL======>${ELASTICSEARCHURL}"
	# ${ELK_Install_Dir}/kibana-${Kibana_Ver}-linux-x86_64/bin/kibana -l ${ELK_Install_Dir}/kibana-${Kibana_Ver}-linux-x86_64/kibana.log --elasticsearch.url=${ELASTICSEARCHURL} --server.host=${CURRNET_IP} &
	# ps -ef | grep "kibana" | grep -v grep | grep -v tail |awk '{print $2}' >${ELK_Install_Dir}/kibana-${Kibana_Ver}-linux-x86_64/pid
	# cat ${ELK_Install_Dir}/kibana-${Kibana_Ver}-linux-x86_64/pid
	# if [ $? -eq 0 ]; then
        # Echo_Green "OK, ====== Kibana_Install Successful!!======"
    # else
		# Echo_Red "Sorry, Kibana_Install failed! please check you operation and try again!"
		# return -1;
	# fi
}

Kibana_Add_AutoStartup(){
	cd ${cur_dir}/src
	\cp ${cur_dir}/init.d/init.d.kibana /etc/init.d/kibana
    chmod +x /etc/init.d/kibana
	sed -i "s#YOURPATH#${PATH}#" /etc/init.d/kibana
	sed -i "s#YOURELK_Install_Dir#${ELK_Install_Dir}#" /etc/init.d/kibana
	sed -i "s#YOURKibana_Ver#${Kibana_Ver}#" /etc/init.d/kibana
    echo "Add kibana to auto startup..."
    StartUp kibana
    #启动kibana
    /etc/init.d/kibana start
	if [ $? -eq 0 ]; then
	    Echo_Green "====== Add kibana Auto Startup Successful! ======"
		Echo_Green "kibana installed successfully, enjoy it!"
    else
        Echo_Red "kibana install failed!"
    fi
}

Uninstall_Kibana()
{
    echo "You will uninstall Kibana..."
	echo "Shutting down Kibana..."
	/etc/init.d/kibana stop
	#Remove_StartUp Kibana
    echo "Delete Kibana files..."
    rm -rf ${ELK_Install_Dir}/kibana*
	rm -rf /etc/init.d/kibana
    Echo_Green "Uninstall Kibana completed."
}